home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1149 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. From: Damian Conway <damian@cs.monash.edu.au>
  2. Message-ID: <199604172311.JAA22065@indy05.cs.monash.edu.au>
  3. X-Original-Date: Thu, 18 Apr 1996 09:11:56 +1000 (EST)
  4. Path: in2.uu.net!bounce-back
  5. Date: 18 Apr 96 01:53:17 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. Subject: Quick question about const ref return types
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMXWgteEDnX0m9pzZAQEcWQF5AUF8JQaDIiRVy4lDLpnB36dpMXTSyJg8
  12.     wZPHCgLNvDNZXmY9bn83HFoFn3APubYu
  13.     =qVnt
  14.  
  15. Is the following legal?
  16.  
  17.     const int& positive(const int& value)
  18.     {
  19.         if (value<0)
  20.         {
  21.             return 0;
  22.         }
  23.         else
  24.         {
  25.             return value;
  26.         }
  27.     }
  28.  
  29. If so, I presume it's also valid in the general case:
  30.  
  31.     const ClassType& func(const ClassType& value);
  32.  
  33. and that the return of a literal generates a temporary which is bound to 
  34. the reference returned and persists until the completion of the full 
  35. expression enclosing the function call (or the destruction of a reference 
  36. variable to which the returned reference is subsequently bound).
  37.  
  38. I've searched through the April 95 WP for a definitive answer.
  39. 8.5.3. [dcl.init.ref] seems to imply this usage is valid (by 
  40. analogy to the binding of temporaries to const reference parameters).
  41.  
  42. damian
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44.   who: Damian Conway               email: damian@bruce.cs.monash.edu.au
  45. where: Dept. Computer Science      phone: +61-3-565-5184
  46.        Monash University             fax: +61-3-565-5146
  47.        Clayton 3168                quote: "A pessimist is never disappointed."
  48.        AUSTRALIA
  49. ---
  50. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  51. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  52. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  53. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  54. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  55.